unit UPharmacyStockSubNew;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, AdvPanel, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinOffice2010Black, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue, cxGroupBox, DBAdvGlowNavigator,
  cxStyles, dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage,
  cxNavigator, Data.DB, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid, UDMPharmacy,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, Vcl.StdCtrls, cxDBEdit, Vcl.Mask, Vcl.DBCtrls, cxCheckBox,
  cxCalendar, Vcl.Menus, cxButtons, UDMItems, UDMSettings, cxRadioGroup;

type
  TFPharmacyStockSubNew = class(TForm)
    AdvPanel1: TAdvPanel;
    AdvPanelStyler1: TAdvPanelStyler;
    cxGroupBox1: TcxGroupBox;
    AdvPanel2: TAdvPanel;
    cxGroupBox3: TcxGroupBox;
    Label2: TLabel;
    cxDBLookupComboBox2: TcxDBLookupComboBox;
    Label3: TLabel;
    cxDBLookupComboBox3: TcxDBLookupComboBox;
    cxGroupBox2: TcxGroupBox;
    Label4: TLabel;
    cxDBTextEdit1: TcxDBTextEdit;
    Label5: TLabel;
    Label6: TLabel;
    cxDBTextEdit2: TcxDBTextEdit;
    cxDBTextEdit3: TcxDBTextEdit;
    cxGroupBox4: TcxGroupBox;
    Label7: TLabel;
    cxDBTextEdit4: TcxDBTextEdit;
    Label8: TLabel;
    cxDBTextEdit5: TcxDBTextEdit;
    Label9: TLabel;
    cxDBTextEdit6: TcxDBTextEdit;
    cxDBTextEdit7: TcxDBTextEdit;
    cxDBTextEdit8: TcxDBTextEdit;
    cxDBTextEdit9: TcxDBTextEdit;
    cxDBTextEdit10: TcxDBTextEdit;
    cxDBTextEdit11: TcxDBTextEdit;
    cxDBTextEdit12: TcxDBTextEdit;
    cxDBTextEdit13: TcxDBTextEdit;
    cxDBTextEdit14: TcxDBTextEdit;
    cxDBTextEdit15: TcxDBTextEdit;
    cxDBTextEdit16: TcxDBTextEdit;
    cxDBTextEdit17: TcxDBTextEdit;
    cxDBTextEdit18: TcxDBTextEdit;
    Label22: TLabel;
    Label1: TLabel;
    cxDBDateEdit1: TcxDBDateEdit;
    Save: TcxButton;
    Label24: TLabel;
    cxLookupComboBox1: TcxLookupComboBox;
    cxGroupBox9: TcxGroupBox;
    Label25: TLabel;
    cxDBTextEdit20: TcxDBTextEdit;
    Label26: TLabel;
    cxDBTextEdit21: TcxDBTextEdit;
    Label27: TLabel;
    cxDBLookupComboBox1: TcxDBLookupComboBox;
    Label28: TLabel;
    cxDBLookupComboBox4: TcxDBLookupComboBox;
    Label29: TLabel;
    cxDBLookupComboBox5: TcxDBLookupComboBox;
    Label30: TLabel;
    cxDBLookupComboBox6: TcxDBLookupComboBox;
    Label31: TLabel;
    cxDBTextEdit22: TcxDBTextEdit;
    Label32: TLabel;
    cxDBTextEdit23: TcxDBTextEdit;
    DBCheckBox3: TDBCheckBox;
    DBCheckBox2: TDBCheckBox;
    DBCheckBox4: TDBCheckBox;
    cxDBRadioGroup2: TcxDBRadioGroup;
    Label33: TLabel;
    cxDBTextEdit24: TcxDBTextEdit;
    Label34: TLabel;
    cxDBTextEdit25: TcxDBTextEdit;
    Label35: TLabel;
    cxDBTextEdit26: TcxDBTextEdit;
    Label36: TLabel;
    Label37: TLabel;
    Label16: TLabel;
    Label38: TLabel;
    Label39: TLabel;
    DBCheckBox1: TDBCheckBox;
    Label10: TLabel;
    cxDBTextEdit19: TcxDBTextEdit;
    cxGroupBox5: TcxGroupBox;
    Label15: TLabel;
    cxCheckBox1: TcxCheckBox;
    Label11: TLabel;
    Label12: TLabel;
    cxDBTextEdit27: TcxDBTextEdit;
    cxDBTextEdit28: TcxDBTextEdit;
    procedure cxDBLookupComboBox2PropertiesEditValueChanged(Sender: TObject);
    procedure SaveClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure cxLookupComboBox1PropertiesEditValueChanged(Sender: TObject);
    procedure cxDBTextEdit1PropertiesEditValueChanged(Sender: TObject);
    procedure cxDBTextEdit3PropertiesEditValueChanged(Sender: TObject);
    procedure cxDBTextEdit24PropertiesEditValueChanged(Sender: TObject);
    procedure cxDBTextEdit25PropertiesEditValueChanged(Sender: TObject);
    procedure calculateTotalQty();
    procedure cxDBTextEdit2PropertiesEditValueChanged(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cxCheckBox1PropertiesEditValueChanged(Sender: TObject);
    procedure cxDBTextEdit19PropertiesEditValueChanged(Sender: TObject);
    procedure cxDBTextEdit27PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FPharmacyStockSubNew: TFPharmacyStockSubNew;
  currency_rate: integer;
  pharmaID:Integer;
  live : Boolean;
  invoiceID : Double;

  expir : Boolean;
  direct :Boolean;
  cost : Double;
  price : Double;
  nr_per_unit : Double;


implementation

{$R *.dfm}

procedure TFPharmacyStockSubNew.SaveClick(Sender: TObject);
  var prevQTY : Double;
  var newQTY : Double;
  var bonusQTY : Double;
  var buttonSelected : Integer;
begin
  pharmaID := Self.cxLookupComboBox1.EditValue;
  //ShowMessage(FloatToStr(pharmaID));
  DMPharmacy.his_pharmacy_stock_sub.FieldByName('pharma_id').Value := pharmaID;
  DMPharmacy.his_pharmacy_stock_sub.FieldByName('currency_rate').Value := currency_rate;
  DMPharmacy.his_pharmacy_stock_sub.FieldByName('supp_invoice_id').Value := invoiceID;
  DMPharmacy.his_pharmacy_stock_sub.Post;
  DMPharmacy.his_pharmacy_stock_sub.RefreshRecord;

  DMPharmacy.his_pharmacy_stock.Locate('parmacy_stock_id',pharmaID, []);
  prevQTY := DMPharmacy.his_pharmacy_stock.FieldByName('qty_in_stock').Value;
  newQTY := DMPharmacy.his_pharmacy_stock_sub.FieldByName('total_unit_qty').Value;
  bonusQTY := DMPharmacy.his_pharmacy_stock_sub.FieldByName('bonus_unit_qty').Value;

  DMPharmacy.his_pharmacy_stock.Edit;
  DMPharmacy.his_pharmacy_stock.FieldByName('qty_in_stock').Value := prevQTY + newQTY + bonusQTY;
  DMPharmacy.his_pharmacy_stock.Post;
  DMPharmacy.his_pharmacy_stock.RefreshRecord;

  Self.cxDBTextEdit25.Enabled := False;
  Self.cxCheckBox1.Checked := False;
  Self.cxLookupComboBox1.ClearSelection;
  buttonSelected := MessageDlg('Do you want to add more Items',mtCustom, [mbYes,mbCancel], 0);
  if buttonSelected = 2 then Self.Close;
  if buttonSelected = 6 then
  begin
    invoiceID := DMPharmacy.his_pharmacy_supplier_invoice.FieldByName('pharmacy_supplier_invoice_id').Value;
    live := true;
    DMPharmacy.his_pharmacy_stock_sub.Insert;
  end;
end;

procedure TFPharmacyStockSubNew.cxCheckBox1PropertiesEditValueChanged(
  Sender: TObject);
  var sell_price:double;
begin
  if live then
  BEGIN
    if self.cxCheckBox1.Checked = True then
    begin
        cxGroupBox4.Enabled := False;
        sell_price := self.cxDBTextEdit19.EditValue;
        Self.cxDBTextEdit4.EditValue := sell_price;
        Self.cxDBTextEdit5.EditValue := sell_price;
        Self.cxDBTextEdit6.EditValue := sell_price;
        Self.cxDBTextEdit7.EditValue := sell_price;
        Self.cxDBTextEdit8.EditValue := sell_price;
        Self.cxDBTextEdit9.EditValue := sell_price;
        Self.cxDBTextEdit10.EditValue := sell_price;
        Self.cxDBTextEdit11.EditValue := sell_price;
        Self.cxDBTextEdit12.EditValue := sell_price;
        Self.cxDBTextEdit13.EditValue := sell_price;
        Self.cxDBTextEdit14.EditValue := sell_price;
        Self.cxDBTextEdit15.EditValue := sell_price;
        Self.cxDBTextEdit16.EditValue := sell_price;
        Self.cxDBTextEdit17.EditValue := sell_price;
        Self.cxDBTextEdit18.EditValue := sell_price;
    end
    else
    begin
        cxGroupBox4.Enabled := True;
        sell_price := self.cxDBTextEdit19.EditValue;
        Self.cxDBTextEdit4.EditValue := '';
        Self.cxDBTextEdit5.EditValue := '';
        Self.cxDBTextEdit6.EditValue := '';
        Self.cxDBTextEdit7.EditValue := '';
        Self.cxDBTextEdit8.EditValue := '';
        Self.cxDBTextEdit9.EditValue := '';
        Self.cxDBTextEdit10.EditValue := '';
        Self.cxDBTextEdit11.EditValue := '';
        Self.cxDBTextEdit12.EditValue := '';
        Self.cxDBTextEdit13.EditValue := '';
        Self.cxDBTextEdit14.EditValue := '';
        Self.cxDBTextEdit15.EditValue := '';
        Self.cxDBTextEdit16.EditValue := '';
        Self.cxDBTextEdit17.EditValue := '';
        Self.cxDBTextEdit18.EditValue := '';
    end;
  END;
end;

procedure TFPharmacyStockSubNew.cxDBLookupComboBox2PropertiesEditValueChanged(
  Sender: TObject);
  var currencyID: integer;
  var currencyLlb : string;
begin
  if live then
  BEGIN
    if cxDBLookupComboBox2.EditValue <> null then
    begin
      currencyLlb := Self.cxDBLookupComboBox2.EditText;
      DMItems.his_currency.Locate('currency_label',currencyLlb, []);
      currencyID := DMItems.his_currency.FieldByName('currency_id').Value;
      currency_rate := DMItems.his_currency.FieldByName('currency_rate').Value;
      Self.Label22.Caption := 'Rate: '+IntToStr(currency_rate);
    end;
  END;
end;

procedure TFPharmacyStockSubNew.cxDBTextEdit19PropertiesEditValueChanged(
  Sender: TObject);
begin
  cxCheckBox1.Enabled := true;
end;

procedure TFPharmacyStockSubNew.cxDBTextEdit1PropertiesEditValueChanged(
  Sender: TObject);
  var discount : Double;
begin
  if live then
  BEGIN
    Self.cxDBTextEdit3.Enabled := True;
    if (self.cxDBTextEdit3.EditValue <> null) then
    begin
      if direct = False then
      begin
        cost := Self.cxDBTextEdit1.EditValue;
        discount := Self.cxDBTextEdit3.EditValue;
        Self.cxDBTextEdit2.EditValue := cost + (cost * discount / 100);
        Self.cxDBTextEdit2.Text := FloatToStr(cost + (cost * discount / 100));
      end
      else
      begin
        cost := Self.cxDBTextEdit1.EditValue;
        discount := Self.cxDBTextEdit3.EditValue;
        Self.cxDBTextEdit2.EditValue := (cost * discount);
      end;
    end;
  END;
end;

procedure TFPharmacyStockSubNew.calculateTotalQty();
 var
    nr_of_pack : double;
    nr_per_pack : double;
    total : Double;
begin
  if live then
  BEGIN
    if (cxDBTextEdit25.EditValue <> null) AND (cxDBTextEdit24.EditValue <> null) then
    begin
      nr_of_pack := Self.cxDBTextEdit24.EditValue;
      nr_per_pack := Self.cxDBTextEdit25.EditValue;
      total := nr_of_pack * nr_per_pack;
      Self.cxDBTextEdit26.EditValue := total;
    end;
  END;
end;

procedure TFPharmacyStockSubNew.cxDBTextEdit24PropertiesEditValueChanged(
  Sender: TObject);
  var
    nr_of_pack : double;
    nr_per_pack : double;
    total : Double;
begin
  if live then
  Begin
    if cxDBTextEdit24.EditValue <> null then
    begin
      Self.cxDBTextEdit25.Enabled := True;
      calculateTotalQty();
    end;
  End;
end;

procedure TFPharmacyStockSubNew.cxDBTextEdit25PropertiesEditValueChanged(
  Sender: TObject);
begin
  if live then
  Begin
    calculateTotalQty();
  End;
end;

procedure TFPharmacyStockSubNew.cxDBTextEdit27PropertiesEditValueChanged(
  Sender: TObject);
  var bonus_qty : double;
begin
  if live then
  BEGIN
    if cxDBTextEdit27.EditValue <> null then
    begin
      bonus_qty := cxDBTextEdit27.EditValue * cxDBTextEdit25.EditValue;
      Self.cxDBTextEdit28.EditValue := bonus_qty;
    end;
  END;
end;

procedure TFPharmacyStockSubNew.cxDBTextEdit2PropertiesEditValueChanged(
  Sender: TObject);
  var
    sell_price : Double;
    qty_per_unit :Double;
    unit_price: Double;
begin
  if live then
  BEGIN
    if cxDBTextEdit2.EditValue <> null then
    begin
      sell_price := self.cxDBTextEdit2.EditValue;
      qty_per_unit := self.cxDBTextEdit25.EditingValue;
      unit_price := sell_price / qty_per_unit;
      Self.cxDBTextEdit19.EditValue := FormatFloat('0.0000', unit_price);
    end;
  END;
end;

procedure TFPharmacyStockSubNew.cxDBTextEdit3PropertiesEditValueChanged(
  Sender: TObject);
  var cost:Double;
  var discount:Double;
begin
  if live then
  BEGIN
    if (cxDBTextEdit3.EditValue <> null) and (cxDBTextEdit1.EditingValue <> null) then
    begin
      if direct = False then
      begin
        cost := Self.cxDBTextEdit1.EditValue;
        discount := Self.cxDBTextEdit3.EditValue;
        Self.cxDBTextEdit2.EditValue := cost + (cost * discount / 100);
        Self.cxDBTextEdit2.Text := FloatToStr(cost + (cost * discount / 100));
      end
      else
      begin
        cost := Self.cxDBTextEdit1.EditValue;
        discount := Self.cxDBTextEdit3.EditValue;
        Self.cxDBTextEdit2.EditValue := (cost * discount);
      end;
    end;
  END;
end;

procedure TFPharmacyStockSubNew.cxLookupComboBox1PropertiesEditValueChanged(
  Sender: TObject);

begin
  if Self.cxLookupComboBox1.SelectedItem <> -1 Then
  begin
    if live then
    BEGIN
      Self.AdvPanel2.Enabled := True;
      DMPharmacy.his_pharmacy_stock.Locate('parmacy_stock_id',self.cxLookupComboBox1.EditValue, []);
      expir := DMPharmacy.his_pharmacy_stock.FieldByName('expirable').Value;
      nr_per_unit := DMPharmacy.his_pharmacy_stock.FieldByName('nr_of_unit').Value;
      direct := DMPharmacy.his_pharmacy_stock.FieldByName('direct_calcule').Value;
      if expir = False then
      BEGIN
        Self.cxDBDateEdit1.Enabled := False;
      END
      else
      begin
        Self.cxDBDateEdit1.Enabled := True;
      end;
      if direct = True then
      BEGIN
        Self.Label5.Caption := 'Multiplier';
      END
      else
      begin
        Self.Label5.Caption := 'Discount';
      end;
      Self.cxDBTextEdit25.EditValue := nr_per_unit;
    END;
  end;
end;

procedure TFPharmacyStockSubNew.FormActivate(Sender: TObject);
begin
  DMPharmacy.his_pharmacy_stock.Refresh;
  invoiceID := DMPharmacy.his_pharmacy_supplier_invoice.FieldByName('pharmacy_supplier_invoice_id').Value;
  live := true;
  DMPharmacy.his_pharmacy_stock_sub.Insert;
end;

procedure TFPharmacyStockSubNew.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  live := False;
  DMPharmacy.his_pharmacy_stock_sub.Cancel;
end;

end.
